home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 November: Tool Chest / Dev.CD Nov 94.toast / New System Software Extensions / OpenDoc A6 / OpenDoc Parts Framework / OPF / Found / FWString / Sources / FWCharIt.cpp < prev    next >
Encoding:
Text File  |  1994-04-21  |  9.5 KB  |  305 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWCharIt.cpp
  4. //    Release Version:    $ 1.0d1 $
  5. //
  6. //    Creation Date:        3/28/94
  7. //
  8. //    Copyright:    © 1994 by Apple Computer, Inc., all rights reserved.
  9. //
  10. //========================================================================================
  11.  
  12. #ifndef FWCHARIT_H
  13. #include "FWCharIt.h"
  14. #endif
  15.  
  16. //========================================================================================
  17. //    CLASS FW_CTextReader
  18. //========================================================================================
  19.  
  20. #ifdef FW_DEBUG_TEXT_ITERATORS
  21. //----------------------------------------------------------------------------------------
  22. //    FW_CTextReader::ClassInvariants
  23. //----------------------------------------------------------------------------------------
  24.  
  25. void FW_CTextReader::ClassInvariants()
  26. {
  27.     if (fNext == 0)
  28.     {
  29.         // Must be in initial state, before first GetNextBuffer
  30.         FW_ASSERT(fStart == 0);
  31.         FW_ASSERT(fLimit == 0);
  32.         FW_ASSERT(fBufferSum == 0);        
  33.     }
  34.     else if (fNext == fLimit)
  35.     {
  36.         // Must be at end of data structure
  37.         FW_ASSERT(fBufferSum == fLength);
  38.     }
  39.     else
  40.     {
  41.         FW_ASSERT(fBufferSum < fLength);
  42.         if (fNext<fStart)
  43.         {
  44.             // Must be at position -1 (backup past beginning)
  45.             FW_ASSERT(fBufferSum == 0);
  46.             FW_ASSERT(fNext == fStart-1); // may not be correct anymore
  47.         }
  48.         else
  49.         {
  50.             // Must be somewhere in middle of stream
  51.             FW_ASSERT(fStart <= fNext);
  52.             FW_ASSERT(fNext <= fLimit);
  53.         }
  54.     }
  55. }
  56. #endif
  57.  
  58. //----------------------------------------------------------------------------------------
  59. //    FW_CTextReader::FW_CTextReader
  60. //----------------------------------------------------------------------------------------
  61.  
  62. FW_CTextReader::FW_CTextReader(const FW_Byte    *chunk1Start, 
  63.                                             const FW_Byte *chunk1Limit,
  64.                                             FW_CharacterCount totalLength) :
  65.     fStart(chunk1Start), 
  66.     fLimit(chunk1Limit), 
  67.     fNext(chunk1Start), 
  68.     fLength(totalLength),
  69.     fBufferSum(0)
  70. {
  71.     ClassInvariants();
  72. }
  73.  
  74. //----------------------------------------------------------------------------------------
  75. //    FW_CTextReader::~FW_CTextReader
  76. //----------------------------------------------------------------------------------------
  77.  
  78. FW_CTextReader::~FW_CTextReader()
  79. {
  80. }
  81.  
  82. //----------------------------------------------------------------------------------------
  83. //    FW_CTextReader::Advance
  84. //----------------------------------------------------------------------------------------
  85.  
  86. void FW_CTextReader::Advance(FW_CharacterCount delta)
  87. {
  88.     ClassInvariants();
  89.     FW_ASSERT(delta>=0);
  90.     FW_ASSERT(delta+GetPosition() <= fLength);
  91.     FW_CharacterCount inThisBuf = FW_CharactersInBlock(fNext, fLimit-fNext);
  92.     while (delta >= inThisBuf)
  93.     {
  94.         delta -= inThisBuf;
  95.         fNext += FW_BytesInString(fNext, inThisBuf);
  96.         GetNextBuffer();
  97.         inThisBuf = fLimit-fStart;
  98.     }
  99.     fNext += FW_BytesInString(fNext, delta);
  100.     ClassInvariants();
  101. }
  102.  
  103. //----------------------------------------------------------------------------------------
  104. //    FW_CTextReader::Backup
  105. //----------------------------------------------------------------------------------------
  106.  
  107. void FW_CTextReader::Backup(FW_CharacterCount delta)
  108. {
  109.     ClassInvariants();
  110.     FW_ASSERT(delta>=0);
  111.     FW_ASSERT(delta<=GetPosition());
  112.     if (fNext==fLimit)
  113.     {
  114.         GetPreviousBuffer();
  115.         fNext = FW_PreviousCharacter(fStart, fLimit);
  116.         delta--;
  117.     }
  118.     FW_CharacterCount inThisBuf = FW_CharactersInBlock(fStart, fNext-fStart);
  119.     while (delta > inThisBuf)
  120.     {
  121.         delta -= inThisBuf;
  122.         fNext = fStart;
  123.         GetPreviousBuffer();
  124.         inThisBuf = FW_CharactersInBlock(fStart, fNext-fStart);
  125.     }
  126.     fNext = FW_PreviousCharacter(fStart, fNext, delta);
  127.     ClassInvariants();
  128. }
  129.  
  130. //----------------------------------------------------------------------------------------
  131. //    FW_CTextReader::SetPosition
  132. //----------------------------------------------------------------------------------------
  133.  
  134. void FW_CTextReader::SetPosition(FW_CharacterCount position)
  135. {
  136.     ClassInvariants();
  137.     FW_ASSERT(position>=0);
  138.     FW_ASSERT(position<=fLength);
  139.     FW_CharacterCount curPosition = GetPosition();
  140.     if (position < curPosition)
  141.         Backup(curPosition-position);
  142.     else if (position > curPosition)
  143.         Advance(position-curPosition);
  144.     ClassInvariants();
  145. }
  146.  
  147. //----------------------------------------------------------------------------------------
  148. //    FW_CTextReader::GetNextBuffer
  149. //----------------------------------------------------------------------------------------
  150.  
  151. void FW_CTextReader::GetNextBuffer()
  152. {
  153.     // ClassInvariants won't hold here.
  154.     // This function is called to (among other things) restore class invariants.
  155.     FW_ASSERT(fNext == fLimit);    // Must hold, but violates invariants
  156.     fBufferSum += FW_CharactersInBlock(fStart, fLimit - fStart);
  157.     if (fBufferSum < fLength)
  158.     {
  159.         DoGetNextBuffer();
  160.         fNext = fStart;
  161.     }
  162.     else
  163.     {
  164.         FW_ASSERT(fBufferSum == fLength);
  165.         fNext = fLimit;
  166.     }
  167.     ClassInvariants();
  168. }
  169.  
  170. //----------------------------------------------------------------------------------------
  171. //    FW_CTextReader::GetPreviousBuffer
  172. //----------------------------------------------------------------------------------------
  173.  
  174. void FW_CTextReader::GetPreviousBuffer()
  175. {
  176.     ClassInvariants();
  177.     if (fBufferSum == 0)
  178.     {
  179.         fNext = fStart;
  180.     }
  181.     else if (fBufferSum == fLength)
  182.     {
  183.         FW_ASSERT(fLimit == fNext);
  184.         fBufferSum -= FW_CharactersInBlock(fStart, fLimit - fStart);
  185.     }
  186.     else
  187.     {
  188.         FW_ASSERT(fStart == fNext);
  189.         DoGetPreviousBuffer();
  190.         FW_ASSERT(fStart < fLimit);
  191.         fBufferSum -= FW_CharactersInBlock(fStart, fLimit - fStart);
  192.         FW_ASSERT(fBufferSum >= 0);
  193.         fNext = fLimit;
  194.     }
  195.     // Invariants won't hold here
  196.     // The calling function must modify fNext appropriately
  197.     // Note that GetPreviousBuffer is a protected method,
  198.     // so clients are not held responsible to restore invariants.
  199.     // Also, subclasses shouldn't need to call this function directly.
  200. }
  201.  
  202. //========================================================================================
  203. //    CLASS FW_CTextWriter
  204. //========================================================================================
  205.  
  206. //----------------------------------------------------------------------------------------
  207. //    FW_CTextWriter::FW_CTextWriter
  208. //----------------------------------------------------------------------------------------
  209.  
  210. FW_CTextWriter::FW_CTextWriter(FW_Byte *chunk1Start, 
  211.                                FW_Byte *chunk1Limit) :
  212.     fStart(chunk1Start), fNext(chunk1Start), fLimit(chunk1Limit), fBufferSum(0)
  213. {
  214. }
  215.  
  216. //----------------------------------------------------------------------------------------
  217. //    FW_CTextWriter::~FW_CTextWriter
  218. //----------------------------------------------------------------------------------------
  219.  
  220. FW_CTextWriter::~FW_CTextWriter()
  221. {
  222. }
  223.  
  224. //========================================================================================
  225. //    CLASS FW_CMemoryReader
  226. //========================================================================================
  227.  
  228. //----------------------------------------------------------------------------------------
  229. //    FW_CMemoryReader::FW_CMemoryReader
  230. //----------------------------------------------------------------------------------------
  231.  
  232. FW_CMemoryReader::FW_CMemoryReader(const FW_Byte* buffer,
  233.                                     FW_ByteCount bytes) :
  234.     FW_CTextReader(buffer, buffer+bytes, FW_CharactersInBlock(buffer,bytes))
  235. {
  236. }
  237.  
  238. //----------------------------------------------------------------------------------------
  239. //    FW_CMemoryReader::FW_CMemoryReader
  240. //----------------------------------------------------------------------------------------
  241.  
  242. FW_CMemoryReader::FW_CMemoryReader(const FW_Char* buffer,
  243.                                     FW_CharacterCount characters) :
  244.     FW_CTextReader((FW_Byte*) buffer, ((FW_Byte*) buffer)+FW_BytesInString(buffer,characters), characters)
  245. {
  246. }
  247.  
  248. //----------------------------------------------------------------------------------------
  249. //    FW_CMemoryReader::~FW_CMemoryReader
  250. //----------------------------------------------------------------------------------------
  251.  
  252. FW_CMemoryReader::~FW_CMemoryReader()
  253. {
  254. }
  255.  
  256. //----------------------------------------------------------------------------------------
  257. //    FW_CMemoryReader::DoGetNextBuffer
  258. //----------------------------------------------------------------------------------------
  259.  
  260. void FW_CMemoryReader::DoGetNextBuffer()
  261. {
  262.     FW_ASSERT(FALSE);
  263. }
  264.  
  265. //----------------------------------------------------------------------------------------
  266. //    FW_CMemoryReader::DoGetPreviousBuffer
  267. //----------------------------------------------------------------------------------------
  268.  
  269. void FW_CMemoryReader::DoGetPreviousBuffer()
  270. {
  271.     FW_ASSERT(FALSE);
  272. }
  273.  
  274. //========================================================================================
  275. //    CLASS FW_CMemoryWriter
  276. //========================================================================================
  277.  
  278. //----------------------------------------------------------------------------------------
  279. //    FW_CMemoryWriter::FW_CMemoryWriter
  280. //----------------------------------------------------------------------------------------
  281.  
  282. FW_CMemoryWriter::FW_CMemoryWriter(FW_Byte* buffer,
  283.                                     FW_ByteCount capacity) :
  284.     FW_CTextWriter(buffer, buffer+capacity)
  285. {
  286. }
  287.  
  288. //----------------------------------------------------------------------------------------
  289. //    FW_CMemoryWriter::~FW_CMemoryWriter
  290. //----------------------------------------------------------------------------------------
  291.  
  292. FW_CMemoryWriter::~FW_CMemoryWriter()
  293. {
  294. }
  295.  
  296. //----------------------------------------------------------------------------------------
  297. //    FW_CMemoryWriter::DoFlushAndGetNextBuffer
  298. //----------------------------------------------------------------------------------------
  299.  
  300. void FW_CMemoryWriter::DoFlushAndGetNextBuffer()
  301. {
  302.     FW_ASSERT(FALSE);    // Overfilled capacity of buffer!
  303. }
  304.  
  305.